///////////////////////////////////////////////////////////
///////////////// Have an itch? Scratch it! ///////////////
///////////////////////// SCRATCH /////////////////////////
/////////////////////  A MUD  Server   ////////////////////
///////////////////// By: Jared Devall ////////////////////
/////////////////////      Thanks:     ////////////////////
/////////////////////  DIKU/Merc/ROM   ////////////////////
///////////////////// Aetas/Deus Gang  ////////////////////
/////////////////////       Beej       ////////////////////
///////////////////////////////////////////////////////////

#include <list>
#include "definitions.h"
#include "commands.h"
#include "commandTable.h"
#include "character.h"
#include "world.h"
#include "stringutil.h"
#include "split.h"
#include "timestamp.h"
#include "vEditHandler.h"

using namespace std;

// oCommand definitions
// (Keep in alphabetical order... it's MUCH easier)
oCmdCommands::oCmdCommands( ) {
	SetName( "commands" );
	SetLevel( 1 );
	SetEnabled( true );
	SetOmniset( false );
}

bool oCmdCommands::Execute( Character* c, const string &args ) {
	std::string buf;
	std::string temp;
	list< Command* >::iterator it;
	int f = 0;

	if ( c->getLevel() >= HEAD ) {
		buf << "\n\r\t{chead (level " << HEAD << ") commands:{x\n\r";
		for ( it = oCommandTable::Instance()._commands.begin(); it != oCommandTable::Instance()._commands.end(); ++it ) {
			if ( (*it)->_level == HEAD ) {
				if ( !(*it)->_enabled )
					buf << "{w";
				if ( f == 0 ) {
					buf << "  " << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				} else {
					buf << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				}
				if ( !(*it)->_enabled )
					buf << "{x";
			    f++;
			    if ( f == 5 ) {
					buf << "\n\r";
					f = 0;
				}
			}
		}
	}
	f = 0;
	if ( c->getLevel() >= GOD ) {
		buf << "\n\r\t{cgod (level " << GOD << ") commands:{x\n\r";
		for ( it = oCommandTable::Instance()._commands.begin(); it != oCommandTable::Instance()._commands.end(); ++it ) {
			if ( (*it)->_level == GOD ) {
				if ( !(*it)->_enabled )
					buf << "{w";
				if ( f == 0 ) {
					buf << "  " << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				} else {
					buf << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				}
				if ( !(*it)->_enabled )
					buf << "{x";
			    f++;
			    if ( f == 5 ) {
					buf << "\n\r";
					f = 0;
				}
			}
		}
	}
	f = 0;
	if ( c->getLevel() >= BUILDER ) {
		buf << "\n\r\t{cbuilder (level " << BUILDER << ") commands:{x\n\r";
		for ( it = oCommandTable::Instance()._commands.begin(); it != oCommandTable::Instance()._commands.end(); ++it ) {
			if ( (*it)->_level == BUILDER ) {
				if ( !(*it)->_enabled )
					buf << "{w";
				if ( f == 0 ) {
					buf << "  " << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				} else {
					buf << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				}
				if ( !(*it)->_enabled )
					buf << "{x";
			    f++;
			    if ( f == 5 ) {
					buf << "\n\r";
					f = 0;
				}
			}
		}
	}
	f = 0;
	buf << "\n\r\t{c-- Commands --{x \n\r";
	for ( it = oCommandTable::Instance()._commands.begin(); it != oCommandTable::Instance()._commands.end(); ++it ) {
		if ( (*it)->_level == 1 ) {
				if ( !(*it)->_enabled )
					buf << "{w";
				if ( f == 0 ) {
					buf << "  " << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				} else {
					buf << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				}
				if ( !(*it)->_enabled )
					buf << "{x";
		    f++;
		    if ( f == 5 ) {
				buf << "\n\r";
				f = 0;
			}
		}
	}


	c->Send( buf );

	return true;
}

/*
oCmd::oCmd() {
	SetName( "" );
	SetLevel( BUILDER );
	SetEnabled( true );
	SetOmniset( false );
}

bool oCmd::Execute( Character* c, const string& args ) {

	if ( args.empty() ) {
		c->Send( "syntax:  <>\n\r" );
		return false;
	}

	c->getObject()->set(  );

	oCmdView cmdView;
	cmdView.Execute( c, "null" );

	return true;
}
*/

oCmdDescription::oCmdDescription() {
	SetName( "description" );
	SetLevel( BUILDER );
	SetEnabled( true );
	SetOmniset( false );
}

bool oCmdDescription::Execute( Character* c, const string& args ) {

	c->setVEdit( true );
	vEditList::Instance().act( c, args );

	return true;
}

oCmdExit::oCmdExit() {
	SetName( "exit" );
	SetLevel( BUILDER );
	SetEnabled( true );
	SetOmniset( false );
}

bool oCmdExit::Execute( Character* c, const string& args ) {

	c->SetStatus( CONNECTED );
	c->setObject( NULL );
	c->Send( "You've been set to regular command mode.\n\r" );

	return true;
}

oCmdLong::oCmdLong() {
	SetName( "long" );
	SetLevel( BUILDER );
	SetEnabled( true );
	SetOmniset( false );
}

bool oCmdLong::Execute( Character* c, const string& args ) {
	std::string tmp = args;

	if ( args.empty() ) {
		c->Send( "syntax: long <string>\n\r" );
		return false;
	}

	c->getObject()->setLong( tmp );

	oCmdView cmdView;
	cmdView.Execute( c, "null" );

	return true;
}

oCmdKeywords::oCmdKeywords() {
	SetName( "keywords" );
	SetLevel( BUILDER );
	SetEnabled( true );
	SetOmniset( false );
}

bool oCmdKeywords::Execute( Character* c, const string& args ) {
	std::string tmp = args;

	if ( args.empty() ) {
		c->Send( "syntax: keywords <keywords>\n\r" );
		return false;
	}

	c->getObject()->setKeywords( tmp );

	oCmdView cmdView;
	cmdView.Execute( c, "null" );

	return true;
}

oCmdShort::oCmdShort() {
	SetName( "short" );
	SetLevel( BUILDER );
	SetEnabled( true );
	SetOmniset( false );
}

bool oCmdShort::Execute( Character* c, const string& args ) {
	std::string tmp = args;

	if ( args.empty() ) {
		c->Send( "syntax: short <string>\n\r" );
		return false;
	}

	c->getObject()->setShort( tmp );

	oCmdView cmdView;
	cmdView.Execute( c, "null" );

	return true;
}

oCmdView::oCmdView() {
	SetName( "view" );
	SetLevel( BUILDER );
	SetEnabled( true );
	SetOmniset( false );
}

bool oCmdView::Execute( Character* c, const string& args ) {

	c->Send( c->getObject()->display() );
	return true;
}
